package me.xiaoyan.java.spring.service;

import me.xiaoyan.java.spring.dao.AccountDao;
import me.xiaoyan.java.spring.dao.UserDao;
import me.xiaoyan.java.spring.po.Account;
import me.xiaoyan.java.spring.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    @Autowired
    public void setUserDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public boolean create(Account account) {

        Account acc = accountDao.findByAccount(account.getAccount());
        if(acc != null){
            throw new RuntimeException("账号已经存在了");
        }
        account.setCreateTime(new Date());
        try{
            accountDao.create(account);
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void update(Account account) {
        accountDao.update(account);

    }

    @Override
    public void delete(Account account) {
        accountDao.delete(account);

    }

    @Override
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Account login(Account account) {
        Account acc = accountDao.findByAccount(account.getAccount());
        if(acc != null){
            if(acc.getPassword().equals(account.getPassword())){
                return acc;
            }
        }
        return null;
    }
}
